package managedbean;

import entity.LineItem;
import entity.Product;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SaleInquiryBeanLocal;


@ManagedBean(name="saleInquiryManagerBean")
@ViewScoped
public class SaleInquiryManagerBean implements Serializable {
    public SaleInquiryManagerBean() {
    }
    
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    
    private Long saleLeadId;
    private List<Integer> quantity;
    private List<LineItem> lineItems;
    private Long saleInquiryId;
    private String statusMessage;
    
    
    public void initView(PhaseEvent event)
    {
        try
        {
            if(lineItems == null)
            {
                List<Product> products = saleInquiryBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);

                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    

    public List<Integer> getQuantity() {
        return quantity;
    }

    public void setQuantity(List<Integer> quantity) {
        this.quantity = quantity;
    }

    public Long getSaleLeadId() {
        return saleLeadId;
    }

    public void setSaleLeadId(Long saleLeadId) {
        this.saleLeadId = saleLeadId;
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
    
    public List<Product> getProduct(){
        return saleInquiryBeanLocal.getProduct();
    }
    
    public void saveSaleInquiry(ActionEvent event){
      
        try{
          List<LineItem> lineItemList = new ArrayList<LineItem>();
          LineItem temp = new LineItem();
          for(int i=0; i<lineItems.size();i++){
              if(lineItems.get(i).getLineItemQuantity()!=null){
                  temp = saleInquiryBeanLocal.createLineItem(lineItems.get(i).getProduct(), lineItems.get(i).getLineItemQuantity());
                  lineItemList.add(temp);
              }
          }
         saleInquiryId = saleInquiryBeanLocal.createSaleInquiry(saleLeadId, lineItemList);
        
         statusMessage = "New Sale Inquiry Saved Successfully"; 
        
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add New Sale Inquiry Result: " + statusMessage + " (New Sale Inquiry ID is " + saleInquiryId.toString() + ")", "")); 
        }
        catch(Exception ex){
         ex.printStackTrace();   
        }
    }
}
